#include "GraphicsUtil.h"
#include <GraphicsBaseItem.h>
#include <QLineF>
#include <qmath.h>
#include <QPainter>
#include <QGraphicsView>

QMap<GraphicsUtil::BorderColor, QColor>  GraphicsUtil::m_borderColorMap = {
	{BorderColor::Cyan_Enum,QColor("#00C5FF")},
	{BorderColor::Blue_Enum,QColor("#221FF6")}
};


QSizeF GraphicsUtil::getKeepRatioSize(qreal width, qreal height, qreal ratio, bool isContainer)
{
	QSizeF keepRatioSize;
	// 以宽为基准，计算图元的高度
	const qreal keepRatioHeight = width / ratio;
	// 以高为基准，计算图元的宽度
	const qreal keepRatioWidth = height * ratio;
	if (isContainer) {
		// 检查设置的宽度是否满足条件
		if (width <= keepRatioWidth) {
			keepRatioSize = QSizeF(keepRatioWidth, height);
		}
		else if (height <= keepRatioHeight) {
			// 检查设置的高度是否满足条件
			keepRatioSize = QSizeF(width, keepRatioHeight);
		}
	}
	else {
		// 检查设置的宽度是否满足条件
		if (width >= keepRatioWidth) {
			keepRatioSize = QSizeF(keepRatioWidth, height);
		}
		else if (height >= keepRatioHeight) {
			// 检查设置的高度是否满足条件
			keepRatioSize = QSizeF(width, keepRatioHeight);
		}
	}
	return keepRatioSize;
}

QPolygonF GraphicsUtil::mapAnglePolygon(const QRectF& rect, qreal angle)
{
	QPolygonF polygon;
	const QPointF center = rect.center();

	QVector<QPointF> points;
	points << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft();
	for (const QPointF& point : points) {
		const QLineF rLine(center, point);
		const qreal radius = rLine.length();
		const qreal dx = point.x() - center.x();
		const qreal dy = point.y() - center.y();

		const qreal x = center.x() + dx * qCos(angle * M_PI / 180) - dy * qSin(angle * M_PI / 180);
		const qreal y = center.y() + dx * qSin(angle * M_PI / 180) + dy * qCos(angle * M_PI / 180);
		polygon.push_back(QPointF(x, y));
	}
	return polygon;
}

QColor GraphicsUtil::getBorderColor(BorderColor borderColor)
{
	return m_borderColorMap[borderColor];
}

void GraphicsUtil::grapItemImage(GraphicsBaseItem* item, QImage& image)
{
	GraphicsBaseItemData& itemData = item->getItemData();
	// 获取 item 的边界矩形
	QSizeF scaleSize = itemData.getScaleSize();

	// 创建一个 QPainter 对象来绘制图像
	QPainter painter(&image);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	QTransform transform;
	if (itemData.IsOriginOnCenter()) {
		transform.translate(scaleSize.width() / 2.0, scaleSize.height() / 2.0);
	}
	const qreal rotateAngle = itemData.getRotateAngle();
	transform.rotate(rotateAngle);
	painter.setTransform(transform);
	// 使用 QGraphicsView 的视图矩阵来绘制图像
	//QGraphicsView* view = qobject_cast<QGraphicsView*>(item->scene()->views().first());
	//if (view) {
	//	painter.setTransform(view->transform());
	//}

	// 将 QGraphicsItem 绘制到 QImage 上
	painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
	item->paint(&painter, nullptr);
	for (GraphicsBaseItem* child : itemData.getChilds()) {
		grapItemImage(child, image);
	}
}

qreal GraphicsUtil::normalizeAngle(qreal angle)
{
	// 将角度标准化到0到360度之间
	qreal normalized = std::fmod(angle, 360.0);
	if (normalized < 0) {
		normalized += 360.0; // 如果结果是负数，加360使其变为正数
	}
	return normalized;
}

QImage GraphicsUtil::roateImage(const QImage& image, qreal angle)
{
	// 创建一个 QImage 对象来存储旋转后的图像
	QImage rotatedImage(image.size(), QImage::Format_ARGB32);
	rotatedImage.fill(Qt::transparent); // 填充透明背景

	// 创建 QPainter 对象用于绘制图像
	QPainter painter(&rotatedImage);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	// 计算图像中心点
	QPointF center(image.width() / 2.0, image.height() / 2.0);

	// 创建一个 QTransform 对象用于旋转
	QTransform transform;
	transform.translate(center.x(), center.y()); // 移动到中心
	transform.rotate(angle); // 旋转
	transform.translate(-center.x(), -center.y()); // 移回原位

	// 应用变换并绘制原始图像到旋转后的图像
	painter.setTransform(transform);
	painter.drawImage(0, 0, image);
	painter.end();

	return rotatedImage;
}

QRect GraphicsUtil::imageBoundingRect(const QImage& image)
{
	int left = image.width();
	int right = 0;
	int top = image.height();
	int bottom = 0;

	for (int y = 0; y < image.height(); ++y) {
		for (int x = 0; x < image.width(); ++x) {
			QColor pixelColor = image.pixelColor(x, y);
			if (pixelColor.alpha() != 0) { // 非完全透明的像素
				left = qMin(left, x);
				right = qMax(right, x);
				top = qMin(top, y);
				bottom = qMax(bottom, y);
			}
		}
	}
	return QRect(QPoint(left, top), QPoint(right, bottom));
}

QImage GraphicsUtil::composeImage(const QImage& mask, const QImage& target)
{
	QImage composeImg(target.size(), QImage::Format_RGBA8888);
	composeImg.fill(Qt::transparent);
	if (mask.isNull())
		return target;
	const QImage scaleMask = mask.scaled(target.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

	QPainter painter(&composeImg);
	painter.drawImage(QPointF(0, 0), target);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
	painter.drawImage(QPointF(0, 0), scaleMask);
	return composeImg;
}
